<?

//require_once(dirname(__FILE__) . '/../abstract/auth.php');

class bf_auth_htpasswd extends bf_auth {

	private $user;

	public function __construct($options = array()) {
		parent::__construct($options);
		$this->opts = array_merge(array(
			'file' => '.htpasswd',
		), $this->opts);
	}

	protected function get_a1($username, $realm) {
		$lines = file($this->opts['file']);
		foreach ($lines as $line) {
			$row = explode(':', rtrim($line));
			if (count($row) < 3)
				continue;
			if ($row[0] != $username)
				continue;
			if ($row[1] != $realm)
				continue;
			$this->user = $row;
			return $row[2];
		}
		return false;
	}

	protected function get_password($username, $realm) {
		return false;
	}

	public function get_value($name) {
		$value = parent::get_value($name);
		if ($value === null)
			$value = ($this->user ? @$this->user[$name] : null);
		return $value;
	}
}

?>